package cn.bjxy.shishaolong.request_answer.service;

import cn.bjxy.shishaolong.request_answer.entity.Answer;
import cn.bjxy.shishaolong.request_answer.entity.Request;
import cn.bjxy.shishaolong.request_answer.mapper.AnswerMapper;
import cn.bjxy.shishaolong.request_answer.mapper.RequestMapper;
import cn.bjxy.shishaolong.request_answer.service.IRequestService;
import cn.bjxy.shishaolong.system.exception.OperatorFailureException;
import cn.bjxy.shishaolong.system.user.entity.SysAdmin;
import cn.bjxy.shishaolong.system.user.entity.SysUserInfo;
import cn.bjxy.shishaolong.system.user.entity.SysUserLocalAuthen;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 时少龙
 * @since 2019-03-16
 */
@Service
public class RequestServiceImpl extends ServiceImpl<RequestMapper, Request> implements IRequestService {

    @Autowired
    private RequestMapper requestMapper;
    @Autowired
    private IAnswerService answerService;

    @Override
    public IPage<Request> queryByCondition(IPage<Request> page, Request request) {
        List<Request> requests = requestMapper.queryByCondition(page, request);
        return page.setRecords(requests);
    }

    @Override
    public List<Request> queryByCondition(Request request) {
        return requestMapper.queryByCondition(request);
    }

    @Override
    public Request queryRequestAnswerById(Integer requestId) {
        // 1.根据id查询问题
        List<Request> requestList = requestMapper.queryByCondition(new Request().setTid(requestId));
        if (Objects.isNull(requestList) || requestList.size() == 0)
            throw new OperatorFailureException(-1, "未查找到该问题的详细信息");

        // 2.根据问题id查询所属的回答
        Request request = requestList.get(0);
        List<Answer> answerList = answerService.queryByCondition(new Answer().setRequestId(requestId));
        request.setAnswerList(answerList);
        return request;
    }

    @Override
    public IPage<Request> queryForAdminByCondition(IPage<Request> page, Request request) {
        List<Request> requestList = requestMapper.queryForAdminByCondition(page, request);
        return page.setRecords(requestList);
    }

    @Override
    public List<Request> queryForAdminByCondition(Request request) {
        List<Request> requestList = requestMapper.queryForAdminByCondition(request);
        return requestList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean remove(String ids) {
        Boolean flag = false;
        for (String id : ids.split(",")) {
            // 对问题进行逻辑删除
            flag = this.update(new UpdateWrapper<Request>().lambda()
                    .set(Request::getDeletedAt, LocalDateTime.now())
                    .set(Request::getUpdateAt, LocalDateTime.now())
                    .eq(Request::getTid, id));

            // 对回答表进行逻辑删除
            flag = answerService.update(new UpdateWrapper<Answer>().lambda()
                    .set(Answer::getDeletedAt, LocalDateTime.now())
                    .set(Answer::getUpdateAt,LocalDateTime.now())
                    .eq(Answer::getRequestId, id));
        }
        return flag;
    }

}
